﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;

namespace Common
{
    public class DataContext : DbContext
    {
        private static DataContext _context = null;
        private static object locker = new object();

        public static DataContext Context
        {
            get
            {
                lock (locker)
                {
                    if (_context == null)
                    {
                        _context = new DataContext();
                    }
                    return _context;
                }
            }
        }

        public DataContext()
        {
            Database.SetInitializer<DataContext>(new DbContextInitializer ());
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Issue>().Ignore<List<Section>>(i => i.Sections);
            modelBuilder.Entity<Section>().Ignore<List<Article>>(s => s.Articles);
            base.OnModelCreating(modelBuilder);
        }

        public DbSet<Attachment> Attachments { get; set; }
        public DbSet<Article> Articles { get; set; } 
        public DbSet<MailMessage> Messages { get; set; }
        public DbSet<Review> Reviews { get; set; }
        public DbSet<CheckedArticle> CheckedArticles { get; set; }
        public DbSet<ReviewedArticle> ReviewedArticles { get; set; }
        public DbSet<Issue> Issues { get; set; }
        public DbSet<Section> Sections { get; set; }
        public DbSet<SectionArticle> SectionArticles { get; set; }
        public DbSet<IssueSection> IssueSections { get; set; }
    } 

    internal class DbContextInitializer : DropCreateDatabaseIfModelChanges<DataContext>
    {
        protected override void Seed(DataContext context)
        {
            #region articles
            //var articles = new List<Article>()
            //{
            //    new Article()
            //    {
            //        AuthorID = "author",
            //        Stage = MaturityStages.AddedToPortfolio,
            //        Tags = "tags tags",
            //        Text = "sample text",
            //        Title = "title sample"
            //    },
            //    new Article()
            //    {
            //        AuthorID = "dmitry",
            //        Stage = MaturityStages.AddedToPortfolio,
            //        Tags = "another tags",
            //        Text = "example of the text",
            //        Title = "brand new article"
            //    }
            //};
            #endregion 
            //var issue = new Issue()
            //{
            //    Title = "test issue plan",
            //    IsPublished = false,
            //    PublishDate = DateTime.Now
            //};

            //var sections = new List<Section>
            //{
            //    new Section()
            //    {
            //        Title = "test#1",
            //    },
            //    new Section()
            //    {
            //        Title = "test#2",
            //    }
            //};
            //var issueSections = new List<IssueSection>();
            //var articleSections = new List<SectionArticle>();
            //int count = 0;
            //foreach (var s in sections)
            //{
            //    issueSections.Add(new IssueSection() { IssueID = issue.ID, SectionID = s.ID });
            //    articleSections.Add(new SectionArticle() { ArticleID = articles[count].ID, SectionID = s.ID });
            //}
            //context.Issues.Add(issue);
            //sections.ForEach(s => context.Sections.Add(s));
            //articles.ForEach(a => context.Articles.Add(a));
            //issueSections.ForEach(Is => context.IssueSections.Add(Is));
            //articleSections.ForEach(As => context.SectionArticles.Add(As));
            //context.SaveChanges();
        }
    }
}